<?php

class AdministrationController extends Zend_Controller_Action {

    public function init() {
        $this->view->baseUrl = $this->_request->getBaseUrl();
    }

    public function indexAction() {
        $this->view->title = "Administration";
        $param = $this->getRequest()->getParams();
        if (isset($param["form"])) {
            $form = $param["form"];
            switch ($form) {
                case 1:
                    $this->addmovie($param);
                    break;
                case 2:
                    $this->addactor($param);
                    break;
                case 3:
                    $this->addmoviehasactor($param);
                    break;
                case 4:
                	$this->deletemovie($param);
   					break; 
   				case 5:
                	$this->deleteactor($param);
   					break; 
   				case 6:
                	$this->updatemovievalidation($param);
   					break; 
   				case 7:
                	$this->updateactorvalidation($param);
   					break;                 
            }
        }
        
        $this->update();
        $this->formMovieHasActor();
    }
    
    public function deletemovie($form) {
    	$movieDB = new Application_Model_Movie;
    	$idMovie = $form['id'];
    	$movie = $movieDB->getMovie($idMovie);
    	$movieDB->deleteMovie($idMovie);
    	
    	$this->view->alert = "Movie '".$movie[0]['Title']."' was deleted";	
    }
    
    public function deleteactor($form) {
    	$actorDB = new Application_Model_Actor;
    	$idActor = $form['id'];
    	$actor = $actorDB->getActor($idActor);
    	$actorDB->deleteActor($idActor);
    	
    	$this->view->alert = "Actor '".$actor[0]['Name']."' was deleted";	
    }
    
    public function updatemovieAction() {
    	$param = $this->getRequest()->getParams();
    	$movieDB = new Application_Model_Movie;
    	$moviehasactorDB = new Application_Model_MoviehasActor;
    	$idMovie = $param['id'];
    	$this->view->movie = $movieDB->getMovie2($idMovie);
    	
    	if(isset($param["act"])) {
    		$moviehasactorDB = new Application_Model_MoviehasActor;
    		$moviehasactorDB->deleteMovieHasActor($param["act"]);
    	}
    }
    
    public function updateactorAction() {
    	$param = $this->getRequest()->getParams();
    	$actorDB = new Application_Model_Actor;
    	$idActor = $param['id'];
    	$this->view->actor = $actorDB->getActor2($idActor);
    }
    
    public function scheduleAction() {
    	$param = $this->getRequest()->getParams();
    	
    	$showingatDB = new Application_Model_ShowingAt;
    	$roomDB = new Application_Model_Room;
    	    		
    	//form Room
    	if(isset($param['formRoom'])) {
    		$roomDB->addRoom();
    		$this->view->alert = "Room added !";
    	}
    	
    	//form Schedule
    	if(isset($param['formSchedule'])) {		
    		$this->view->alert = "The movie was added in the schedule";
    		
    		$date = $param["Date"];
    		$Date = substr($date, 6, 4) . "-" .
                substr($date, 0, 2) . "-" .
                substr($date, 3, 2). " ".$param["Hour"].":". $param["Min"].":00";
    		$showingatDB->addShowAt($param['Movie_ID'], $param['Room_ID'], $Date);
    	}
    	
    	if(isset($param['dls'])) {	
    		$this->view->alert = "Show deleted";
    		$showingatDB->deleteShow($param['dls']);
    	}
    	
    	if(isset($param['dlr'])) {	
    		$this->view->alert = "Room deleted";
    		$roomDB->deleteRoom($param['dlr']);
    	}
    	
        $movieDB = new Application_Model_Movie;
        $this->view->movies = $movieDB->getMovies();
        
        $roomDB = new Application_Model_Room;
        $this->view->rooms = $roomDB->getRooms();

    	$this->view->shows = $showingatDB->getShows();
    }

    public function addmovie($form) {
        $this->view->alert = "Movie '" . $form['Title'] . "' added !";
        $Title = $form['Title'];
        $ESRB = $form['ESRB'];
        $Description = $form["Description"];

        $date = $form["DateRelease"];
        $D_Release = substr($date, 6, 4) . "-" .
                substr($date, 0, 2) . "-" .
                substr($date, 3, 2);
        
        $RunningTime = $form["RunningTime"];

        if (!empty($_FILES['poster'])) {
            $picture_temp = $_FILES['poster']['tmp_name'];
            $picture = $_FILES['poster']['name'];
            $Poster = time() . $picture;
            move_uploaded_file($picture_temp, "posters/" . $Poster);
        }
        
        $Ranking = 0;
        $movieDB = new Application_Model_Movie;
        $movieDB->addMovie($Title, $ESRB, $D_Release, $Poster, $Ranking, $RunningTime, $Description);
    }
    
    public function updatemovievalidation($form) {
        $this->view->alert = "Movie '" . $form['Title'] . "' updated !";
        $Movie_ID = $form['Movie_ID'];
        $Title = $form['Title'];
        $ESRB = $form['ESRB'];
        $Description = $form["Description"];

        $date = $form["DateRelease"];
        $D_Release = substr($date, 6, 4) . "-" .
                substr($date, 0, 2) . "-" .
                substr($date, 3, 2);
        
        $RunningTime = $form["RunningTime"];

		$Poster = $form["poster"];
		
        if (!empty($_FILES['newposter']) && $_FILES['newposter']['name'] != '') {
            $picture_temp = $_FILES['newposter']['tmp_name'];
            $picture = $_FILES['newposter']['name'];
            $Poster = time() . $picture;
            move_uploaded_file($picture_temp, "posters/" . $Poster);
        }
        
        $Ranking = 0;
        $movieDB = new Application_Model_Movie;
        $movieDB->updateMovie($Movie_ID, $Title, $ESRB, $D_Release, $Poster, $Ranking, $RunningTime, $Description);
    }

 	public function updateactorvalidation($form) {
        $this->view->alert = "Actor '" . $form['name'] . "' updated !";
        $Actor_ID = $form['Actor_ID'];
        $Name = $form['name'];
        $date = $form["birthday"];
        $Birthday = substr($date, 6, 4) . 
                substr($date, 0, 2) .
                substr($date, 3, 2);
        
        $Picture = $form['picture'];
        
         if (!empty($_FILES['newpicture']) && $_FILES['newpicture']['name'] != '') {
            $picture_temp = $_FILES['newpicture']['tmp_name'];
            $picture = $_FILES['newpicture']['name'];
            $Picture = time() . $picture;
            move_uploaded_file($picture_temp, "pictures/" . $Picture);
        }
        
        $actorDB = new Application_Model_Actor;
        $actorDB->updateActor($Actor_ID, $Name, $Birthday, $Picture);
        
        
    }
    
    public function addactor($form) {
        $this->view->alert = "Actor '" . $form['name'] . "' added !";
        $Name = $form['name'];
        $date = $form["birthday"];
        $Birthday = substr($date, 6, 4) . 
                substr($date, 0, 2) .
                substr($date, 3, 2);
        
        
         if (!empty($_FILES['picture'])) {
            $picture_temp = $_FILES['picture']['tmp_name'];
            $picture = $_FILES['picture']['name'];
            $Picture = time() . $picture;
            move_uploaded_file($picture_temp, "pictures/" . $Picture);
        }
        
        $actorDB = new Application_Model_Actor;
        $actorDB->addActor($Name, $Birthday, $Picture);
        
        
        
        
    }

    public function addmoviehasactor($form) {
    	$movieDB = new Application_Model_Movie;
    	$actorDB = new Application_Model_Actor;
    	$moviehasactorDB = new Application_Model_MoviehasActor;
    	
    	$idActor = $form["actor"];
    	$idMovie = $form["movie"];
        
        $actor = $actorDB->getActor($idActor);
        $movie = $movieDB->getMovie($idMovie);
        
        $moviehasactorDB->addMhasA($idMovie, $idActor);
        
        $this->view->alert = $actor[0]['Name']." is now playing in ".$movie[0]['Title']." !";
        
    }

    public function formMovieHasActor() {
        $actorDB = new Application_Model_Actor;
        $this->view->actors = $actorDB->getActors();

        $movieDB = new Application_Model_Movie;
        $this->view->movies = $movieDB->getMovies();
    }
    
    public function update() {
        $movieDB = new Application_Model_Movie;
        $this->view->moviesUpdate = $movieDB->getMovies2();
        
        $actorDB = new Application_Model_Actor;
        $this->view->actorsUpdate = $actorDB->getActors2();
    }

}

